
include('graphics.node');
include('events');

new_class(
  'graphics.Animation',
  function(duration) {
    graphics.Node.call(this);
    this.duration = duration;
    this.timer = 0;
  }, {
    start: function(node) {
      node.addAnimation(this);
      this.timer = 0;
      this.notify('didStart');
    },
    stop: function() {
      this.node.removeAnimation(this);
      this.notify('didStop');
    },
    reset: function(node) {
      this.timer = 0;
    },
    _step: function(d, dt) {
      // Slightly different than a regular node.
      this.timer += dt;
      if (this.timer >= this.duration) {
        dt = this.duration - this.timer;
        this.timer = this.duration;
      }
      this.update(dt);
      this._children.each(function(i,o) {
        if (o)
          o._step(d, dt);
      });
      if (this.timer >= this.duration) {
        this.stop();
      }
    }
  },
  graphics.Node);

add_events_to_class(graphics.Animation);


new_class(
  'graphics.AnimateScale',
  function(duration, from, to) {
    if (from !== undefined && to === undefined) {
      to = from;
      from = undefined;
    }

    this.parent(duration);
    this.scaleFrom = from;
    this.scaleTo = to;
  }, {
    start: function(node) {
      if (this.scaleFrom === undefined) this.scaleFrom = node.scaleX;
      if (this.scaleTo === undefined) this.scaleTo = node.scaleX;
      this.ratio = (this.scaleTo - this.scaleFrom) / this.duration;
      this.super.start.call(this, node);
    },
    update: function(dt) {
      this.node.scaleX = this.scaleFrom + this.ratio * this.timer;
      this.node.scaleY = this.scaleFrom + this.ratio * this.timer;
    }
  },
  graphics.Animation);
